<template>
  <div class="user-info-container">
    <div class="header-bar">
      <router-link to="/home" class="back-button">
        <img src="@/assets/images/mine/back.png" alt="返回图标" class="return-image" />
      </router-link>
      <h1 class="page-title">修改个人信息</h1>
      <div class="placeholder"></div>
    </div>
    
    <form @submit.prevent="handleSubmit" class="user-form">
      <div class="form-group">
        <div class="form-item">
          <label for="nickname" class="form-label">昵称</label>
          <div class="input-wrapper">
            <input 
              id="nickname" 
              v-model="nickname"
              class="form-input"
              placeholder="请输入昵称"
            />
          </div>
        </div>
        
        <div class="form-item">
          <label for="gender" class="form-label">性别</label>
          <div class="custom-select">
            <div 
              class="select-display" 
              @click="toggleDropdown"
            >
              {{ selectedGender === '1' ? '男' : '女' }}
              <span class="select-arrow"></span>
            </div>
            <div class="select-options" v-if="isDropdownOpen">
              <div 
                class="select-option" 
                :class="{ 'active': selectedGender === '1' }"
                @click="selectGender('1')"
              >
                男
              </div>
              <div 
                class="select-option" 
                :class="{ 'active': selectedGender === '0' }"
                @click="selectGender('0')"
              >
                女
              </div>
            </div>
          </div>
        </div>
        
        <div class="form-item">
          <label for="email" class="form-label">邮箱</label>
          <div class="input-wrapper">
            <input 
              type="email" 
              id="email" 
              v-model="email"
              class="form-input"
              placeholder="请输入邮箱"
            />
          </div>
        </div>
      </div>
      
      <button type="submit" class="submit-button">
        <span class="button-text">保存修改</span>
      </button>
    </form>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { useUserStore } from '@/stores/user';
import { saveChanges } from '../api/index';
import { showToast } from 'vant';
const userStore = useUserStore();

const nickname = ref('');
const selectedGender = ref('');
const email = ref('');
const isDropdownOpen = ref(false);

onMounted(() => {
  nickname.value = userStore.userInfo.nickname;
  selectedGender.value = String(userStore.userInfo.gender);
  email.value = userStore.userInfo.email;
  
  // 添加点击外部关闭下拉框的监听
  document.addEventListener('click', closeDropdownOnClickOutside);
});

onBeforeUnmount(() => {
  // 移除事件监听以避免内存泄漏
  document.removeEventListener('click', closeDropdownOnClickOutside);
});

const toggleDropdown = (event) => {
  event.stopPropagation();
  isDropdownOpen.value = !isDropdownOpen.value;
};

const selectGender = (value) => {
  selectedGender.value = value;
  isDropdownOpen.value = false;
};

const closeDropdownOnClickOutside = (event) => {
  if (isDropdownOpen.value && !event.target.closest('.custom-select')) {
    isDropdownOpen.value = false;
  }
};

const handleSubmit = async () => {
  const updatedInfo = {
    id: userStore.userInfo.id,
    nickname: nickname.value,
    gender: selectedGender.value,
    email: email.value
  };
  try {
    const response = await saveChanges(updatedInfo);
    if (response.data.code === 200) {
      userStore.userInfo.nickname = updatedInfo.nickname;
      userStore.userInfo.gender = updatedInfo.gender;
      userStore.userInfo.email = updatedInfo.email;
      showToast('修改成功');  
    } else {
      alert('信息更新失败: ' + response.data.message);
    }
  } catch (error) {
    console.error('保存信息失败:', error);
    alert('保存信息失败，请稍后重试');
  }
};
</script>

<style lang="scss" scoped>  
.user-info-container {  
  min-height: 100vh;  
  width: 100%;  
  background: linear-gradient(135deg, #a78bfa, #6366F1);  
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;  
  color: #fff;  
  display: flex;  
  flex-direction: column;  
}  

.header-bar {  
  position: relative;
  height: 56px;  
  display: flex;  
  align-items: center;  
  justify-content: space-between;  
  padding: 0 16px;  
}  

.back-button {  
  display: flex;  
  align-items: center;  
  justify-content: center;  
  width: 36px;  
  height: 36px;  
  border-radius: 50%;  
  background-color: rgba(255, 255, 255, 0.2);  

  .return-image {  
    width: 20px;  
    height: 20px;  
    filter: brightness(0) invert(1);  
  }  
}  

.page-title {  
  font-size: 18px;  
  font-weight: 600;  
  margin: 0;  
  position: absolute; /* 使用绝对定位 */
  left: 50%; /* 水平居中 */
  transform: translateX(-50%); /* 调整位置 */
  text-align: center;  
}  

.user-form {  
  flex: 1;  
  display: flex;  
  flex-direction: column;  
  padding: 24px 20px;  
}  

.form-group {  
  background-color: rgba(255, 255, 255, 0.1);  
  border-radius: 16px;  
  padding: 20px;  
  backdrop-filter: blur(10px);  
  margin-bottom: 24px;  
}  

.form-item {  
  margin-bottom: 20px;  

  &:last-child {  
    margin-bottom: 0;  
  }  
}  

.form-label {  
  font-size: 15px;  
  font-weight: 500;  
  margin-bottom: 8px;  
  color: rgba(255, 255, 255, 0.9);  
}  

.input-wrapper {  
  position: relative;  
  width: 100%;  
}  

.form-input {  
  width: 100%;  
  padding: 14px 16px;  
  border: 1px solid rgba(255, 255, 255, 0.3);  
  border-radius: 12px;  
  background-color: rgba(255, 255, 255, 0.15);  
  color: #fff;  
  font-size: 16px;  
  transition: all 0.3s ease;  
  box-sizing: border-box;

  &::placeholder {  
    color: rgba(255, 255, 255, 0.5);  
  }  

  &:focus {  
    outline: none;  
    border-color: rgba(255, 255, 255, 0.6); 
    background-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);  
  }  
}  

// 自定义下拉菜单样式
.custom-select {
  position: relative;
  width: 100%;
  z-index: 10;
}

.select-display {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  box-sizing: border-box;
  position: relative;
  transition: all 0.3s ease;
  
  &:hover {
    background-color: rgba(255, 255, 255, 0.2);
  }
  
  .select-arrow {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%) rotate(45deg);
    width: 10px;
    height: 10px;
    border-right: 2px solid rgba(255, 255, 255, 0.8);
    border-bottom: 2px solid rgba(255, 255, 255, 0.8);
    pointer-events: none;
  }
}

.select-options {
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  background-color: #fffffff0;
  border-radius: 12px;
  margin-top: 5px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 20;
}

.select-option {
  padding: 14px 16px;
  color: #333;
  cursor: pointer;
  transition: background-color 0.2s;
  
  &:hover {
    background-color: #f5f5f5;
  }
  
  &.active {
    background-color: #ffffff59;
    color: #6366F1;
    font-weight: 500;
  }
}

.submit-button {  
  background: linear-gradient(135deg, #a78bfa, #6366F1);  
  border: none;  
  border-radius: 14px;  
  height: 52px;  
  display: flex;  
  align-items: center;  
  justify-content: center;  
  box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4);  
  cursor: pointer;  
  transition: all 0.3s ease;  
  margin-top: auto;  

  .button-text {  
    color: #fff;  
    font-size: 17px;  
    font-weight: 600;  
    letter-spacing: 0.5px;  
  }  

  &:active {  
    transform: translateY(2px);  
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);  
  }  
}  
</style>  